package com.zzyl.serve.service.impl;

import java.util.List;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zzyl.common.utils.DateUtils;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.service.INursingProjectPlanService;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 护理计划Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-10-04
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService
{
    @Autowired
    private NursingPlanMapper nursingPlanMapper;
    @Autowired
    private INursingProjectPlanService nursingProjectPlanService;
    @Autowired
    private RedisTemplate redisTemplate;
    private static final String NURSINGPLAN_ALL = "nursingPlan:all";



    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    public NursingPlanVo selectNursingPlanById(Integer id)
    {
        //1.查询护理计划
        NursingPlan plan = this.getById(id);
        //2.查询护理项目计划中间表
        List<NursingProjectPlan> list = nursingProjectPlanService.list(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, id));
        //3.数据封装
        NursingPlanVo planVo = BeanUtil.toBean(plan, NursingPlanVo.class);
        List<NursingProjectPlanVo> nursingProjectPlanVoList = list.stream().map(nursingProjectPlan -> {
            NursingProjectPlanVo nursingProjectPlanVo = BeanUtil.toBean(nursingProjectPlan, NursingProjectPlanVo.class);
            nursingProjectPlanVo.setProjectId(nursingProjectPlan.getProjectId().toString());
            nursingProjectPlanVo.setExecuteCycle(nursingProjectPlan.getExecuteCycle().toString());
            return nursingProjectPlanVo;
        }).collect(Collectors.toList());
        planVo.setProjectPlans(nursingProjectPlanVoList);
        return planVo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan)
    {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNursingPlan(NursingPlanDto nursingPlanDto)
    {
        deleteCache();
        //1.新增护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);


        //2.新增护理项目计划中间表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(CollectionUtils.isNotEmpty(projectPlans)){
            List<NursingProjectPlan> projectPlans1 = projectPlans.stream().filter(nursingProject -> nursingProject.getProjectId() != null)
                    .peek(nursingProject -> nursingProject.setPlanId(Long.valueOf(nursingPlan.getId()))).collect(Collectors.toList());
            if(!projectPlans1.isEmpty()){
                boolean b = nursingProjectPlanService.saveBatch(projectPlans1);
            }
        }
        boolean save = this.save(nursingPlan);
        return save?1:0;
    }

    //删除缓存
    private void deleteCache() {
        redisTemplate.delete(NURSINGPLAN_ALL);
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNursingPlan(NursingPlanDto nursingPlanDto)
    {
        deleteCache();
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);

        //2.修改护理项目计划中间表
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        //2.1删除护理项目计划中间表
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId, nursingPlan.getId()));
        //2.2批量添加护理项目计划中间表
        if (CollectionUtils.isNotEmpty(projectPlans)) {
            List<NursingProjectPlan> validPlans = projectPlans.stream()
//                    .filter(p -> p.getProjectId() != null && p.getProjectId() > 0)
                    .filter(p -> p.getProjectId() != null)
                    .peek(p -> p.setPlanId(Long.valueOf(nursingPlan.getId())))
                    .collect(Collectors.toList());

            if (!validPlans.isEmpty()) {
                boolean saved = nursingProjectPlanService.saveBatch(validPlans);
            }
        }
        //1.修改护理计划
        boolean b = this.updateById(nursingPlan);
        return b?1:0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteNursingPlanByIds(Integer[] ids)
    {
        deleteCache();
        return removeByIds(Arrays.asList(ids))==true?1:0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNursingPlanById(Integer id)
    {
        deleteCache();
        //1.删除护理计划
        boolean b = this.removeById(id);
        //2.删除护理项目计划中间表
        nursingProjectPlanService.remove(Wrappers.<NursingProjectPlan>lambdaQuery().eq(NursingProjectPlan::getPlanId,id));
        return b?1:0;
    }


    /**
     * 查询所有护理计划
     *
     * @return 护理计划集合
     */
    @Override
    public List<NursingPlanVo> getPlanAll() {
        //先去缓存中查询
        List<NursingPlanVo> list = (List<NursingPlanVo>) redisTemplate.opsForValue().get(NURSINGPLAN_ALL);
        if (CollectionUtil.isNotEmpty(list)) {
            return list;
        }
        list = nursingPlanMapper.getPlanAll();
        redisTemplate.opsForValue().set(NURSINGPLAN_ALL, list);
        return list;
    }
}
